<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar-box">
        <img src="http://weldon.net.cn/back/img/logo2.fc048282.png" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginForm"
        label-width="0px"
        class="login-form"
        :model="form"
        :rules="rules"
      >
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="el-icon-user-solid" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            type="password"
          />
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";
export default {
  name: "Login-page",
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      console.log(this);
      this.$refs.loginForm.resetFields();
    },
    async login() {
      try {
        await this.$refs.loginForm.validate(); // validate是element-ui组件的form表单方法
        const res = await login(this.form); // 发起登录请求
        if (res.meta.status !== 200) return this.$message.error("登陆失败");
        this.$message.success("登陆成功");
        console.log(res.data.token);
        this.$store.commit("user/setUserToken", res.data.token); // 把token存到本地仓库vuex
        this.$router.push("/"); // 路由跳转页面
      } catch (e) {
        console.log(e);
      }
    },
  },
};
</script>

<style>
.login-container {
  height: 100%;
  background-color: #2b4b6b;
}
.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
}
.avatar-box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 5px;
  box-shadow: 0 0 10px #ddd;
  margin: 0 auto;
  transform: translateY(-50%);
  background-color: #fff;
}
.avatar-box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
form {
  display: block;
  margin-top: 0em;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
